package swagenda.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.apache.log4j.Logger;

import swagenda.data.Person;
import swagenda.proxy.OosProxy;
import swagenda.ui.components.SWAButton;
import swagenda.ui.components.SWALabel;
import swagenda.ui.components.SWATextField;

public class PrefsDialog extends JDialog
{
	Logger logger = Logger.getLogger(this.getClass());
	private static final long serialVersionUID = 20070914L;

	JTextField slugField = new SWATextField();
	JTextField emailField = new SWATextField();
	JTextPane message = new JTextPane();
	
	JButton next = new SWAButton("next");
	JButton cancel = new SWAButton("cancel");
	
	JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
	JPanel fieldsPanel = new JPanel();
	OosProxy oos;

	public PrefsDialog(Frame frameOwner, OosProxy oos)
	{
		super(frameOwner);
		this.oos = oos;
		
		if (oos.owner==null) oos.owner=new Person();

		setModal(true);
		setBounds(400, 250, 0, 0);
		setTitle("preferences");
		getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
		
		fieldsPanel.setLayout(new BoxLayout(fieldsPanel, BoxLayout.PAGE_AXIS));
		
		JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p1.add(new SWALabel("user slug"));
		slugField.setText(oos.owner.slug);
		p1.add(slugField);
		//p1.setBorder(new LineBorder(Color.GREEN));
		fieldsPanel.add(p1);
		
		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p2.add(new SWALabel("email"));
		emailField.setText(oos.owner.email);
		p2.add(emailField);
		//p2.setBorder(new LineBorder(Color.RED));
		fieldsPanel.add(p2);
		//fieldsPanel.setBorder(new LineBorder(Color.BLACK));
		getContentPane().add(fieldsPanel);
		
		buttons.add(next);
		buttons.add(cancel);
		getContentPane().add(buttons);
		
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				nextPressed(evt);
			}
		});
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				setVisible(false);
			}
		});
	}
	
	private void nextPressed(ActionEvent e)
	{
		logger.debug("prefs dialog next pressed");
		JButton auth = new SWAButton("authorize");
		try	
		{
			oos.owner.slug = slugField.getText(); 
			oos.owner.email = emailField.getText(); 
			oos.getTempToken();
			
			message = new JTextPane();
			message.setText("swagenda requires your authorization to access your data on 11870.com, please note that you grant access to this application at your own risk, and this application is provided \"as is\".");
			message.setFont(new Font("Dialog", Font.PLAIN, 16));
			message.setOpaque(false);
			message.setEditable(false);
			message.setPreferredSize(new Dimension(350,120));
			fieldsPanel.removeAll();
			fieldsPanel.add(message);
			buttons.removeAll();
			buttons.add(auth);
			buttons.add(cancel);
			auth.addActionListener(new ActionListener() {
	            public void actionPerformed(ActionEvent evt) {
	            	authPressed(evt);
	            }
	        });
			
			pack();
		}
		catch(Exception ex)
		{
			logger.error(ex);
		}
	}

	private void authPressed(ActionEvent e)
	{
		logger.debug("prefs dialog auth pressed");
		oos.authenticate();
		message.setText("Done! click on the \"complete authorization\" button so you can start rocking swagenda!");
		buttons.removeAll();
		SWAButton complete = new SWAButton("complete authorization");
		buttons.add(complete);
		buttons.add(cancel);
		complete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	completePressed(evt);
            }
        });
		
	}

	private void completePressed(ActionEvent e)
	{
		try	
		{
			logger.debug("prefs dialog complete pressed");
			oos.owner.authToken = oos.getAuthToken();
			oos.reload();
			setVisible(false);
			((MainFrame)getOwner()).reload();

		}
		catch(Exception ex)
		{
			logger.error(ex);
		}
	}
}
